package com.example.onlinemusic.controller;

import com.example.onlinemusic.model.Music;
import com.example.onlinemusic.model.User;
import com.example.onlinemusic.service.LoveMusicService;
import com.example.onlinemusic.tools.Constant;
import com.example.onlinemusic.tools.ResponseBodyMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/lovemusic")
public class LoveMusicController {

    @Autowired
    private LoveMusicService loveMusicService;

    //查看该音乐是否被收藏
    @RequestMapping("/likemusic")
   public ResponseBodyMessage<Boolean> likeMusic (@RequestParam String id, HttpServletRequest request){

        int musicId = Integer.parseInt(id);
        System.out.println("musicId" + musicId);

        HttpSession session = request.getSession(false);
        if(session == null || session.getAttribute(Constant.USERINFO_SESSION_KEY) == null){
            System.out.println("没有登录");
            return new ResponseBodyMessage<>(-1,"请登录后上传",false);
        }
        User user = (User) session.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();
        System.out.println("userId" + userId);
        Music music = null;
                 music = loveMusicService.getLoveMusic(userId,musicId);
        if(music != null){
            return new ResponseBodyMessage<>(-1,"该音乐已被收藏,不能再收藏",false);
        }
        //如果 music 等于 null
        boolean flg = loveMusicService.CollectionMusic(userId,musicId);
        //判断为真就收藏成功,未假就收藏失败
        if(flg){
            return new ResponseBodyMessage<>(0,"该音乐已收藏",true);
        }else {
            return new ResponseBodyMessage<>(-1,"收藏失败",false);
        }

   }

   @RequestMapping("/findlovemusic")
   public ResponseBodyMessage<List<Music>>  findLoveMusicByUserId(@RequestParam(required = false)
                                                                          String musicName,
                                                                  HttpServletRequest request){

       HttpSession session = request.getSession(false);
       if(session == null || session.getAttribute(Constant.USERINFO_SESSION_KEY) == null){
           System.out.println("没有登录");
           return new ResponseBodyMessage<>(-1,"请登录后查询",null);
       }
       User user = (User) session.getAttribute(Constant.USERINFO_SESSION_KEY);
       int userid = user.getId();
       System.out.println("userid :" + userid);

       List<Music> musicList = null;
       if (musicName == null){
           musicList = loveMusicService.findLoveMusicByUserId(userid);
       }else {
           musicList = loveMusicService.findLoveMusicByMusicName(musicName,userid);
       }
       return new ResponseBodyMessage<>(0,"已查询到你收藏的音乐",musicList);
   }

   @RequestMapping("/deletelovemusic")
   public ResponseBodyMessage<Boolean>  deleteLoveMusic(@RequestParam String id,
                                                        HttpServletRequest request){
        int musicId = Integer.parseInt(id);
       HttpSession session = request.getSession(false);
       if(session == null || session.getAttribute(Constant.USERINFO_SESSION_KEY) == null){
           System.out.println("没有登录");
           return new ResponseBodyMessage<>(-1,"请登录后移除",null);
       }
       User user = (User) session.getAttribute(Constant.USERINFO_SESSION_KEY);
       int userId = user.getId();
       System.out.println("userid :" + userId);

       int ret = loveMusicService.deleteLoveMusic(userId,musicId);
       if(ret == 1){
           return new ResponseBodyMessage<>(0,"取消收藏成功",true);
       }else {
           return new ResponseBodyMessage<>(-1,"取消收藏失败",false);
       }
   }

}
